package com.example.guideapp.activity;

import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.example.guideapp.R;
import com.example.guideapp.adapter.MyDayListViewAdapter;
import com.example.guideapp.utils.Constant;
import com.example.guideapp.utils.DatabaseManage;
import com.example.guideapp.utils.MySqliteDatabaseHelper;

import java.util.Calendar;

public class AddMyFestivalActivity extends AppCompatActivity {

    private EditText et_choose_name;
    private EditText et_add_time;
    private EditText et_choose_remark;
    private Button btn_add;

    private MySqliteDatabaseHelper helper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_my_festival);

        initView();

        initClick();

    }

    private void initClick() {
        et_add_time.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    showDatePickDlg();
                    return true;
                }
                return false;
            }
        });

        et_add_time.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    showDatePickDlg();
                }
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = helper.getWritableDatabase();

                String  name = et_choose_name.getText().toString().trim();
                String date = et_add_time.getText().toString().trim();

                if (name.equals("") || date.equals("")){
                    Toast.makeText(AddMyFestivalActivity.this, "名称或日期不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                ContentValues cv = new ContentValues();
                cv.put(Constant.MYFESTIVAL_NAME,name);
                cv.put(Constant.MYFESTIVAL_DATE,date);
                cv.put(Constant.MYFESTIVAL_REMARK,et_choose_remark.getText().toString());
                long b = db.insert(Constant.TABLE_MYFESTIVAL_NAME,null,cv);

                db.close();
                finish();
            }
        });
    }

    private void initView() {
        helper = DatabaseManage.getInstance(this);

        et_choose_name = (EditText) findViewById(R.id.et_choose_name);
        et_add_time = (EditText) findViewById(R.id.et_add_time);
        et_choose_remark = (EditText) findViewById(R.id.et_choose_remark);
        btn_add = (Button) findViewById(R.id.btn_add);

        SpannableString spannableString = new SpannableString("备注...");
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(15,true);
        spannableString.setSpan(absoluteSizeSpan,0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        et_choose_remark.setHint(new SpannedString(spannableString));
    }

    private void showDatePickDlg() {
        Calendar c = Calendar.getInstance();
        DatePickerDialog dpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                et_add_time.setText(year+"-"+month+"-"+dayOfMonth);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
        dpd.show();
    }
}
